package com.bole.service.impl;

import com.bole.dao.AdminDao;
import com.bole.dao.MenuDao;
import com.bole.dao.MenuRoleDao;
import com.bole.pojo.TbMenuDo;
import com.bole.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yakamaxi
 * @version 1.0
 * @date 2020/8/12 10:41
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private MenuRoleDao menuRoleDao;

    @Override
    public List<Map<String, Object>> getMenu(Long id) {
        List<TbMenuDo> list = menuDao.getMenuByUserId(id);
        return getMenuTree(list, 0L);
    }

    @Override
    public List<Map<String, Object>> getzTree() {
        List<TbMenuDo> list = menuDao.findAll();
        return getMenuZTree(list, 0L);
    }
    
    @Override
    public void addMenu(TbMenuDo menu) {
        menuDao.save(menu);
    }

    @Override
    public TbMenuDo getMenuById(Long id) {
        return menuDao.getById(id);
    }

    @Override
    public void updateById(TbMenuDo menu) {
        menuDao.save(menu);
    }

    @Override
    public void deleteById(Long id) {
        menuDao.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> getzTreeChecked(Long rid) {
        // 查询所有的节点(查询树)
        List<TbMenuDo> list = menuDao.findAll();
        // 查询当前角色对应中间表的数据(当前角色对应中间表中的mid)
        List<Long> mids = menuRoleDao.selectRoleMenuByRid(rid);
        // 拿到对应数据 进行对比 (打钩)
        return getMenuZTreeChecked(list, 0L, mids);
    }

    public List<Map<String, Object>> getMenuZTreeChecked(List<TbMenuDo> list, Long parentId, List<Long> mids) {// 0  1
        // 定义一个集合进行数据的接收
        List<Map<String, Object>> mapList = new ArrayList<>();
        // 遍历数据
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = null;
            TbMenuDo menu = list.get(i);
            if (parentId.equals(menu.getPid())) {
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("name", menu.getName());
                if (mids != null && mids.size() > 0 && mids.contains(menu.getId())) {
                    map.put("checked", true);
                }
                // 参数 当前对象的id当做父级id找到子节点进行拼接
                map.put("children", getMenuZTreeChecked(list, menu.getId(), mids));
            }
            if (map != null) {
                List<Map<String, Object>> childrenList = (List<Map<String, Object>>) map.get("children");
                if (childrenList.size() <= 0) {
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;//[]
    }

    private List<Map<String, Object>> getMenuTree(List<TbMenuDo> list, Long parentId) {// 0  1
        // 定义一个集合进行数据的接收
        List<Map<String, Object>> mapList = new ArrayList<>();
        // 遍历数据
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = null;
            TbMenuDo menu = list.get(i);
            if (parentId.equals(menu.getPid())) {
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("url", menu.getUrl());
                map.put("text", menu.getName());
                // 参数 当前对象的id当做父级id找到子节点进行拼接
                map.put("nodes", getMenuTree(list, menu.getId()));
            }
            if (map != null) {
                mapList.add(map);
            }
        }
        return mapList;
    }

    private List<Map<String, Object>> getMenuZTree(List<TbMenuDo> list, Long parentId) {// 0  1
        // 定义一个集合进行数据的接收
        List<Map<String, Object>> mapList = new ArrayList<>();
        // 遍历数据
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = null;
            TbMenuDo menu = list.get(i);
            if (parentId.equals(menu.getPid())) {
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("name", menu.getName());
                // 参数 当前对象的id当做父级id找到子节点进行拼接
                map.put("children", getMenuZTree(list, menu.getId()));
            }
            if (map != null) {
                List<Map<String, Object>> childrenList = (List<Map<String, Object>>) map.get("children");
                if (childrenList.size() <= 0) {
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;//[]
    }


}
